<?php
abstract class flCommon {
	
	static function truncate($text, $length, $ending = ' ...', $exact = false) {		 	
		$len = $length - mb_strlen($ending, 'utf-8');		
		$chars = $text;
		if ($len > mb_strlen($text, 'utf-8')) {
			$len = mb_strlen($text, 'utf-8');
		}				    
		$i = 0;
		$m = 0;
		$n = 0;
		$k = 0;		
		do{			
			if (preg_match ('/[0-9a-zA-Z]|[\s]|[!"#$%&\'()*+,-.\/:;<=>?@[\]^_`{|}~]/', $chars[$i])){//纯英文   
				$m++;
			} else {				
				$n++;
			}//非英文字节,
			$k = $n/3+$m;
			$l = $n/3+$m;//最终截取长度；$l = $n/3+$m*2？
			$i++;
		} while($k < $len);
		$str1 = mb_substr($text,0, $l, 'utf-8');//保证不会出现乱码			
		if (!$exact) {
			$spacepos = mb_strrpos($str1, ' ', 'utf-8');			
			if ($spacepos !== false) {
				$str1 = mb_substr($str1, 0, $spacepos, 'utf-8'); 
			}
		}			
		if ($str1 != $text)
			$str1 = $str1.$ending;	   
		return $str1;		
	}
}